<?php 

require_once("database.include.php");
		
$messageId = $_GET['messageId'];

//debug only
//echo 'messageId: ' . $messageId . '<br/>';

if (empty($messageId) ) {
	echo 'no data';
	return 'no data';
	exit;
}


$inputErrors = validateInput($messageId);

if (strcmp('', $inputErrors) == 0 ) {
	//echo 'calling createGroup';
	return deleteGroupMessage ($conn, $messageId);
}
else {
	echo  "Delete group message failed." . $inputErrors;
	return ("Delete group message failed." . $inputErrors);
}

function validateInput($messageId) {
	$retVal = '';
	if (empty($messageId)) {
		$retVal .= 'messageId cannot be empty.';
	}
	
	echo $retVal;
	return $retVal;
}


function deleteGroupMessage( $conn, $messageId ) {

	try {		
		$conn->beginTransaction();
		//Create new group		
		$stmt = $conn->prepare(' DELETE FROM `message` WHERE message_id = ? ');
		$sendMessageSuccess = $stmt->execute( array( $messageId ) );				
		$conn->commit();

		if ( $stmt->rowCount() == 1 ) {		
			echo "<br/>Success on deleting message: " . $messageId;
			return "<br/>Success on deleting message: " . $messageId;
		}
		else {
			echo "<br/>Message id does not exist: " . $messageId;
			return "<br/>Message id does not exist: " . $messageId;
		}
	}	
	catch(PDOException $err) {
		$conn->rollBack();
		echo "<br/>Failure deleting message: " . $messageId . ', ' . $err->getMessage();		
		return "<br/>Failure deleting message: " . $messageId;
	}
	catch(Exception $err) 
	{
		$conn->rollBack();
		echo "<br/>Internal failure deleting message: " . $messageId;
		return "Internal failure deleting message: " . $messageId;
	}
}
?>
